package com.dungeon.game;

import java.util.HashMap;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.LightingColorFilter;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.util.Log;

public class GameImages {

	static HashMap<String,GameImage> image_list;
	LaunchActivity c;

	void init(LaunchActivity context){
		c = context;
		image_list = new HashMap<String,GameImage>();
		image_list.put("red",GameImage.make_singlecolor_overlay(60,255,0,0));
		image_list.put("light_blue",GameImage.make_singlecolor_overlay(60,150,150,250));
		image_list.put("green",GameImage.make_singlecolor_overlay(60,0,255,0));
		image_list.put("grey",GameImage.make_singlecolor_overlay(150,100,100,100));
		image_list.put("whitesquare",GameImage.make_singlecolor_overlay(150,250,250,250));
		image_list.put("pale",GameImage.make_singlecolor_overlay(40,255,255,255));
		image_list.put("verypale",GameImage.make_singlecolor_overlay(80,255,255,255));
		image_list.put("red_frame",GameImage.make_frame_overlay(255,0,0));
		image_list.put("green_frame",GameImage.make_frame_overlay(0,255,0));
		image_list.put("blue_frame",GameImage.make_frame_overlay(0,0,255));
	}
	
	boolean add(String image_name){
		boolean failed = true;
		String name="";
    	for(String n:LaunchActivity.imageFileNames){
    		if (n.equals(image_name+".png") || n.equals(image_name+".bmp")){
    			name = n;
    			failed = false;
    			break;
    		}
    	}	    		
    	if (failed){
    		Log.e("Demo","Missing image: " + image_name);
    		return false;
    	}
		GameImage gi = new GameImage();
		BitmapDrawable bd = c.getDrawableFromAsset(c,"images/"+name);
		Bitmap base = bd.getBitmap();
		gi.add(GameImage.image_types.IT_BASIC,base);
		Bitmap shrunk = Bitmap.createScaledBitmap(base,base.getWidth()*5/12,base.getHeight()*5/12,false);
		gi.add(GameImage.image_types.IT_SMALL,shrunk);
		Bitmap very_shrunk = Bitmap.createScaledBitmap(base,base.getWidth()*3/12,base.getHeight()*3/12,false);
		gi.add(GameImage.image_types.IT_TINY,very_shrunk);
		image_list.put(name.substring(0,name.lastIndexOf('.')),gi);
		return true;
	}

   	public Bitmap getImageBasic(String image_name){
		if (image_list.get(image_name)==null){
			if (!add(image_name))
			return Bitmap.createBitmap(96,96,Bitmap.Config.ARGB_8888);
		} 
		GameImage gi = (GameImage)image_list.get(image_name);
		return gi.get(GameImage.image_types.IT_BASIC);
    }

   	public Bitmap getImageSmall(String image_name){
		if (image_list.get(image_name)==null){
			if (!add(image_name))
			return Bitmap.createBitmap(96,96,Bitmap.Config.ARGB_8888);
		} 
		GameImage gi = (GameImage)image_list.get(image_name);
		return gi.get(GameImage.image_types.IT_SMALL);		
    }
   	
   	public Bitmap getImageTiny(String image_name){
		if (image_list.get(image_name)==null){
			if (!add(image_name))
			return Bitmap.createBitmap(96,96,Bitmap.Config.ARGB_8888);
		} 
		GameImage gi = (GameImage)image_list.get(image_name);
		return gi.get(GameImage.image_types.IT_TINY);
    }   	

   	public Bitmap getImageDark(String image_name){
		if (image_list.get(image_name)==null){
			if (!add(image_name))
			return Bitmap.createBitmap(96,96,Bitmap.Config.ARGB_8888);
		} 
		GameImage gi = (GameImage)image_list.get(image_name);
		if (!gi.i.containsKey(GameImage.image_types.IT_DARK))
			gi.i.put(GameImage.image_types.IT_DARK,darken(gi.get(GameImage.image_types.IT_BASIC)));
		return gi.get(GameImage.image_types.IT_DARK);
    }

   	public Bitmap getImageGrey(String image_name){
		if (image_list.get(image_name)==null){
			if (!add(image_name))
			return Bitmap.createBitmap(96,96,Bitmap.Config.ARGB_8888);
		} 
		GameImage gi = (GameImage)image_list.get(image_name);
		if (!gi.i.containsKey(GameImage.image_types.IT_GREY))
			gi.i.put(GameImage.image_types.IT_GREY,grey_out(gi.get(GameImage.image_types.IT_BASIC)));
		return gi.get(GameImage.image_types.IT_GREY);
    }
   	
   	public Bitmap getImageRed(String image_name){
		if (image_list.get(image_name)==null){
			if (!add(image_name))
			return Bitmap.createBitmap(96,96,Bitmap.Config.ARGB_8888);
		} 
		GameImage gi = (GameImage)image_list.get(image_name);
		if (!gi.i.containsKey(GameImage.image_types.IT_RED))
			gi.i.put(GameImage.image_types.IT_RED,shade_red(gi.get(GameImage.image_types.IT_BASIC)));
		return gi.get(GameImage.image_types.IT_RED);
    }

  	public Bitmap getImageGreen(String image_name){
		if (image_list.get(image_name)==null){
			if (!add(image_name))
			return Bitmap.createBitmap(96,96,Bitmap.Config.ARGB_8888);
		} 
		GameImage gi = (GameImage)image_list.get(image_name);
		if (!gi.i.containsKey(GameImage.image_types.IT_GREEN))
			gi.i.put(GameImage.image_types.IT_GREEN,more_green(gi.get(GameImage.image_types.IT_BASIC)));
		return gi.get(GameImage.image_types.IT_GREEN);
    }

   	public Bitmap getImageWhite(String image_name){
		if (image_list.get(image_name)==null){
			if (!add(image_name))
			return Bitmap.createBitmap(96,96,Bitmap.Config.ARGB_8888);
		} 
		GameImage gi = (GameImage)image_list.get(image_name);
		if (!gi.i.containsKey(GameImage.image_types.IT_WHITE))
			gi.i.put(GameImage.image_types.IT_WHITE,white_out(gi.get(GameImage.image_types.IT_BASIC)));
		return gi.get(GameImage.image_types.IT_WHITE);
    }

   	static Bitmap darken(Bitmap bmpOriginal){
   	    int width, height;
   	    height = bmpOriginal.getHeight();
   	    width = bmpOriginal.getWidth();    
   	    Bitmap bmpGrayscale = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
   	    Canvas c = new Canvas(bmpGrayscale);
   	    Paint paint = new Paint();
   	    LightingColorFilter f = new LightingColorFilter(0xFF444444,0);
   	    paint.setColorFilter(f);
   	    c.drawBitmap(bmpOriginal, 0, 0, paint);
   	    return bmpGrayscale;   	
   	}
   	
   	static Bitmap grey_out(Bitmap bmpOriginal){
   	    int width, height;
   	    height = bmpOriginal.getHeight();
   	    width = bmpOriginal.getWidth();    
   	    Bitmap bmpGrayscale = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
   	    Canvas c = new Canvas(bmpGrayscale);
   	    Paint paint = new Paint();
   	    ColorMatrix cm = new ColorMatrix();
   	    cm.setSaturation(0);
   	    ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm);
   	    paint.setColorFilter(f);
   	    c.drawBitmap(bmpOriginal, 0, 0, paint);
   	    return bmpGrayscale;   	
   	}

   	static Bitmap shade_red(Bitmap bmpOriginal){
   	    int width, height;
   	    height = bmpOriginal.getHeight();
   	    width = bmpOriginal.getWidth();    
   	    Bitmap bmpGrayscale = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
   	    Canvas c = new Canvas(bmpGrayscale);
   	    Paint paint = new Paint();
   	    ColorMatrix cm = new ColorMatrix();
   	    cm.setScale(1.5f,1f,1f,1f);
   	    ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm);
   	    paint.setColorFilter(f);
   	    c.drawBitmap(bmpOriginal, 0, 0, paint);
   	    return bmpGrayscale;   	
   	}
   	
   	static Bitmap white_out(Bitmap bmpOriginal){
   	    int width, height;
   	    height = bmpOriginal.getHeight();
   	    width = bmpOriginal.getWidth();   
   	    Bitmap bmpWhite = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
   	    Canvas c = new Canvas(bmpWhite);
   	    Paint paint = new Paint();
   	    LightingColorFilter f = new LightingColorFilter(0xFFFFFFFF,0xFFFFFFFF);
   	    paint.setColorFilter(f);
   	    c.drawBitmap(bmpOriginal, 0, 0, paint);
   	    return bmpWhite;   	
   	}     	
   	
   	static Bitmap more_green(Bitmap bmpOriginal){
   	    int width, height;
   	    height = bmpOriginal.getHeight();
   	    width = bmpOriginal.getWidth();   
   	    Bitmap bmpWhite = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
   	    Canvas c = new Canvas(bmpWhite);
   	    Paint paint = new Paint();
   	    LightingColorFilter f = new LightingColorFilter(0xFFAAFFAA,0x00002200);
   	    paint.setColorFilter(f);
   	    c.drawBitmap(bmpOriginal, 0, 0, paint);
   	    return bmpWhite;   	
   	}   	

}
